import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  Provide,
  Query,
  ALL,
  Del,
  Put,
  Patch,
} from '@midwayjs/decorator';

import { BaseController, ERRCODE } from '../../../comm/controller';
import {
  AddressPageDto,
  CreateAddressDto,
  UpdateAddressDto,
} from '../dto/address';
import { CustomerAddressService } from '../service/address';

@Provide()
@Controller('/mall/address')
export class MallAddressController extends BaseController {
  @Inject()
  customerAddressService: CustomerAddressService;

  @Post('/add')
  async add(@Body(ALL) address: CreateAddressDto) {
    try {
      const data = await this.customerAddressService.add(address);
      return this.ok({ address: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }

  @Post('/delete')
  @Del('/delete')
  async delete(@Body('id') addressId: number) {
    try {
      await this.customerAddressService.delete(addressId);
      return this.ok({});
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }

  @Post('/update')
  @Put('/update')
  @Patch('/update')
  async update(@Body(ALL) address: UpdateAddressDto) {
    try {
      const data = await this.customerAddressService.update(address);
      return this.ok({ address: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }

  @Get('/info')
  async info(@Query('id') addressId) {
    try {
      const data = await this.customerAddressService.info(addressId);
      return this.ok({ address: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }

  @Get('/page')
  async page(@Query(ALL) pageOp: AddressPageDto) {
    try {
      const data = await this.customerAddressService.page(pageOp);
      return this.ok({ addresses: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }
}
